package com.senthink.gl.business.userlog.business;

import com.senthink.gl.business.area.AreaRequest;
import com.senthink.gl.business.domain.entity.Area;
import com.senthink.gl.business.domain.entity.UserLog;
import com.senthink.gl.business.domain.service.AreaDataService;
import com.senthink.gl.business.fault.DeviceTypeEnum;
import com.senthink.gl.business.fault.FaultRequest;
import com.senthink.gl.business.userlog.SystemUserLog;
import com.senthink.gl.business.userlog.enums.LogDetailEnum;
import com.senthink.gl.business.userlog.enums.LogOperateEnum;
import com.senthink.gl.business.userlog.enums.LogTypeEnum;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 除设备外其他操作日志记录
 *
 * @author yuanpeng@senthink.com
 * @data 2018-02-05 11:49
 */

@Component
public class LogOtherBusiness {

    @Autowired
    private AreaDataService areaDataService;

    public void main(SystemUserLog systemUserLog, UserLog userLog, HttpServletRequest request, JoinPoint joinPoint) {
        if (systemUserLog.logType() == LogTypeEnum.AREA) {
            areaMessages(systemUserLog, userLog, request, joinPoint);
        } else if (systemUserLog.logType() == LogTypeEnum.POLICIES) {
            if (systemUserLog.operate() == LogOperateEnum.CONTROL) {
                //downPolicies(systemUserLog, userLog, joinPoint);
            } else {
                //policiesMessages(systemUserLog, userLog, joinPoint, request);
            }
        } else if (systemUserLog.logType() == LogTypeEnum.POLICIES_PARAM) {
            //policiesParam(systemUserLog, userLog);
        } else if (systemUserLog.logType() == LogTypeEnum.SYSTEM) {
            systemLog(systemUserLog, userLog, joinPoint);
        } else if (systemUserLog.logType() == LogTypeEnum.FAULT) {
            faultLog(systemUserLog, userLog, joinPoint);
        }
    }

    /**
     * 区域操作详情
     *
     * @param systemUserLog
     * @param userLog
     * @param joinPoint
     */
    private void areaMessages(SystemUserLog systemUserLog, UserLog userLog, HttpServletRequest request, JoinPoint joinPoint) {
        if (systemUserLog.operate() == LogOperateEnum.DELETE) {
            Area area = areaDataService.findByAid(request.getParameter("aid"))
                    .orElse(null);
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + (area == null ? "unknown" : area.getName()));
        } else {
            AreaRequest areaRequest = (AreaRequest) joinPoint.getArgs()[0];
            Area area = areaDataService.findByName(areaRequest.getName())
                    .orElse(null);
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + (area == null ? "unknown" : area.getName()));
        }
    }

    /**
     * 故障等级操作
     *
     * @param systemUserLog
     * @param userLog
     * @param joinPoint
     */
    private void faultLog(SystemUserLog systemUserLog, UserLog userLog, JoinPoint joinPoint) {
        if (systemUserLog.operate() == LogOperateEnum.DELETE) {
            FaultRequest request = (FaultRequest) joinPoint.getArgs()[0];
            //Fault fault = faultDataService.findById(request.getId()).orElse(null);
            //String detail = fault == null ? "unknown" : DeviceTypeEnum.fromType(fault.getDeviceType()).getDeviceName();
            //userLog.setDetails(systemUserLog.detail().getDetail() + ":" + detail);
        } else if (systemUserLog.operate() == LogOperateEnum.UPDATE || systemUserLog.operate() == LogOperateEnum.CREATE) {
            FaultRequest request = (FaultRequest) joinPoint.getArgs()[0];
            userLog.setDetails(systemUserLog.detail().getDetail() + ":" + DeviceTypeEnum.fromType(request.getDeviceType()).getDeviceName());
        }
    }
    /**
     * 系统设置相关操作获取操作详情
     *
     * @param systemUserLog
     * @param userLog
     * @param joinPoint
     */
    private void systemLog(SystemUserLog systemUserLog, UserLog userLog, JoinPoint joinPoint) {
        if (systemUserLog.detail() == LogDetailEnum.UPDATE_OTHER_SYSTEM) {
        } else if (systemUserLog.detail() == LogDetailEnum.DELETE_OTHER_SYSTEM) {
        } else if (systemUserLog.detail() == LogDetailEnum.CREATE_OTHER_SYSTEM) {
        } else if (systemUserLog.detail() == LogDetailEnum.UPDATE_SYSTEM_CONFIG) {
            userLog.setDetails(systemUserLog.detail().getDetail());
        }
    }

}
